﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneCreator2 : BaseGame {


	public GameObject staffLeft = null;
	public GameObject staffRight = null;

	public GameObject staffsLeft = null;
	public GameObject staffsRight = null;

	public float originY = -340;

	private static SceneCreator2 instance = null;
	public static SceneCreator2 GetInstance()
	{
		return instance;
	}

	void Awake()
	{
		instance = this;
	}

	void Start()
	{
//		originY = staffsLeft.transform.localPosition.y;
	}
		

	public override void Init( string id )
	{
		gameCfg = DataManager.gameCfgData.GetCfg (id);
		if (gameCfg == null)
			return;

		music.clip = null;

		// 关闭游戏1
		SceneCreator.GetInstance().CloseGame();
		// 打开游戏2
		gameObject.SetActive(true);

		ReInit ();
	}



	bool preparedReady = false;
	public override void ReInit()
	{
		if (gameCfg == null)
			return;

		nowGame = this;

		Clear ();

		hasBegin = false;
		preparedReady = false;

		gameUI.GetInstance ().HideReward ();
		basketCollierCheck.CloseGuide ();

		staffsLeft.transform.localPosition = new Vector3 (staffsLeft.transform.localPosition.x, originY, 0);
		staffsRight.transform.localPosition = new Vector3 (staffsRight.transform.localPosition.x, originY, 0);


		score = 0;

		bool firstLeft = true;
		bool firstRight = true;
		GameObject lastone = null;
		GameObject firstone = null;
		// 直接创造所有水果
		foreach (var item in gameCfg.gudian) {
		
			GameObject go = Instantiate (staffLeft) as GameObject;
			go.SetActive (true);
			go.name = "staffLeft";
			go.transform.parent = staffLeft.transform.parent;
			go.transform.localScale = staffLeft.transform.localScale;
			go.transform.localRotation = staffLeft.transform.localRotation;
			go.transform.localPosition = staffLeft.transform.localPosition + new Vector3 (0,item * gameCfg.speed*100, 0);

			if (firstLeft) {
				firstone = go;
				firstLeft = false;
			}

			lastone = go;
		}

		foreach (var item in gameCfg.gudian2) {

			GameObject go = Instantiate (staffRight) as GameObject;
			go.SetActive (true);
			go.name = "staffRight";
			go.transform.parent = staffRight.transform.parent;
			go.transform.localScale = staffRight.transform.localScale;
			go.transform.localRotation = staffRight.transform.localRotation;
			go.transform.localPosition = staffRight.transform.localPosition + new Vector3 (0,item * gameCfg.speed*100, 0);

			if (firstRight) {
				
				firstRight = false;

				if (go.transform.localPosition.y < firstone.transform.localPosition.y) {
					go.tag = "firstRight";
				}
				else
					firstone.tag = "firstLeft";
			}

			if ( go.transform.localPosition.y > lastone.transform.localPosition.y)
				lastone = go;
		}

		lastone.tag = "lastGudian";

		if (music.clip != null)
			preparedReady = true;
//			BeginGame ();
		else {
		
			if (!string.IsNullOrEmpty (gameCfg.urlMusic)) {
				Loading.GetInstance ().StartLoading ();
				ResLoader.Load(gameCfg.urlMusic, null, (www)=>{
					AudioClip ac = null;
					if ( www.error == null )
						ac = WWWAudioExtensions.GetAudioClip (www);

					music.clip = ac;
					Loading.GetInstance().EndLoading();

//					preparedReady = true;
					Invoke("DelayPrepareReady", 0.3f);
				}, true, true);
			}
		}
	}

	public void DelayPrepareReady()
	{
		preparedReady = true;
	}
		
	void FixedUpdate()
	{
		if (preparedReady)
			BeginGame ();

		Debug.Log ("hasbegin = " + hasBegin.ToString());
		if (!hasBegin)
			return;

		float deltaDistance = Time.fixedDeltaTime* gameCfg.speed * 100;
		staffsLeft.transform.position= staffsLeft.transform.position - new Vector3 (0, deltaDistance, 0);
		staffsRight.transform.position= staffsRight.transform.position - new Vector3 (0, deltaDistance, 0);

		Debug.Log (staffsRight.transform.position + "        time:" + music.time);
	}


	public override void BeginGame()
	{
		preparedReady = false;

		if(gameUI.GetInstance() != null )
			gameUI.GetInstance ().RefreshScore (0, gameCfg.gudian.Count);

		music.Stop ();
		Debug.Log ("music play");
		music.Play ();

		

		hasBegin = true;
	}



	public GameObject basketLeft = null;
	public GameObject basketRight = null;
	public void OnBtnLeft()
	{
		if (basketCollierCheck.nowStaffLeft != null ) {
			// operate
			LeanTween.moveY(basketCollierCheck.nowStaffLeft, gameCfg.speed * 100*1.5f , 0.3f).setDestroyOnComplete(true).tweenType = LeanTweenType.easeOutExpo;
			LeanTween.alphaCanvas (basketCollierCheck.nowStaffLeft.GetComponent<CanvasGroup> (), 0.1f, 0.3f);
			

			basketCollierCheck.nowStaffLeft = null;

			LeanTween.scale (basketLeft, new Vector3 (1.2f, 1.2f, 1), 0.1f).setLoopPingPong (1).tweenType = LeanTweenType.easeOutExpo;

			// close guide
			basketCollierCheck.CloseGuide();
			BaseGame.nowGame.UnPause ();

			// add score
			AddScore();
		}
		
	}

	public void OnBtnRight()
	{
		if (basketCollierCheck.nowStaffRight != null ) {
			// operate
			LeanTween.moveY(basketCollierCheck.nowStaffRight, gameCfg.speed * 100*1.5f, 0.3f).setDestroyOnComplete(true).tweenType = LeanTweenType.easeOutExpo;
			LeanTween.alphaCanvas (basketCollierCheck.nowStaffRight.GetComponent<CanvasGroup> (), 0.1f, 0.3f);
			basketCollierCheck.nowStaffRight = null;

			LeanTween.scale (basketRight, new Vector3 (1.2f, 1.2f, 1), 0.1f).setLoopPingPong (1).tweenType = LeanTweenType.easeOutExpo;

			// close guide
			basketCollierCheck.CloseGuide();
			BaseGame.nowGame.UnPause ();

			// add score
			AddScore();
		}
	}


	public override void Clear()
	{
		// 销毁之前的
		for (int i = staffLeft.transform.parent.childCount -1; i >0; i--) {
		
			GameObject o = staffLeft.transform.parent.GetChild (i).gameObject;
			GameObject.Destroy (o);
		}

		// 销毁之前的
		for (int i = staffRight.transform.parent.childCount -1; i >0; i--) {

			GameObject o = staffRight.transform.parent.GetChild (i).gameObject;
			GameObject.Destroy (o);
		}

		Resources.UnloadUnusedAssets ();
	}


	public override void CloseGame()
	{
		Clear ();

		music.clip = null;

		gameObject.SetActive (false);
	}

	

	public override void Replay()
	{
		Time.timeScale = 1;

		ReInit ();
	}



}
